package org.framework.common.api.support.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.annotation.JsonAutoDetect;

/**
 * ECharts3图表模型
 * <p>描述: </p>
 * <p>Company: Professional</p>
 * @author chanlong(陈龙)
 * @date 2016年9月12日 下午1:05:48
 * @version 1.0.2016
 */
@JsonAutoDetect
public class EChartsModel implements Serializable{

	/** serialVersionUID(long):. */
	private static final long serialVersionUID = 1187678889423234159L;
	
	// 轴数据
	private List<Object> axis;
	
	// 图例数据
	private List<String> legend;
	
	// 时间轴数据
	private List<String> timeline;
	
	// 系列数据
	private Map<String, Object> series;
	
	/**
	 * 构建图表数据.
	 * @param map
	 * @return
	 * @author chanlong(陈龙)
	 * @date 2016年9月16日 上午6:43:30
	 */
	@SuppressWarnings("unchecked")
	public Object create(final Map<String, Object> map) {
		if(timeline != null && !timeline.isEmpty()){
			for (String key : timeline) {
				addSeries(key, (List<Map<String, Object>>)map.get(key));
			}
		}
		return this;
	}
	
	/**
	 * 添加图例.
	 * @param legend
	 * @return
	 * @author chanlong(陈龙)
	 * @date 2016年9月18日 下午3:18:37
	 */
	public EChartsModel addLegend(final String... legend){
		if(this.legend == null) this.legend = new ArrayList<String>();
		
		if(legend != null){
			for (String value : legend) {
				this.legend.add(value);
			}
		}
		
		return this;
	}
	
	/**
	 * 添加时间轴.
	 * @return
	 * @author chanlong(陈龙)
	 * @date 2016年9月18日 下午3:19:47
	 */
	public EChartsModel addTimeline(final String type, final int interval){
		if(this.timeline == null) this.timeline = new ArrayList<String>();
		
		Calendar calendar = Calendar.getInstance();
		
		switch (type) {
			case "month":
				int month = calendar.get(Calendar.MONTH)+1;
				for(int i = 0; i < interval; i++){
					this.timeline.add((month-i)+"月");
				}
			break;
		}
		
		return this;
	}
	
	/*
	 * 添加系列数据
	 */
	private void addSeries(final String key, final List<Map<String, Object>> result){
		if(series == null) series = new LinkedHashMap<String, Object>();
		
		List<Map<String, List<Object>>> list = new ArrayList<Map<String, List<Object>>>();
		
		for (String _legend : legend) {
			Map<String, List<Object>> data = new LinkedHashMap<String, List<Object>>();
			data.put(_legend, new ArrayList<Object>());
			
			for (Map<String, Object> _result : result) {
				data.get(_legend).add(_result.get(_legend));
				
				addAxis(_result.get("axis"));
			}
			
			list.add(get(data.get(_legend)));
		}
		
		series.put(key, list);
	}
	
	/*
	 * 获取数据
	 */
	private Map<String, List<Object>> get(final List<Object> data){
		Map<String, List<Object>> map = new HashMap<String, List<Object>>();
		map.put("data", data);
		return map;
	}
	
	/*
	 * 添加轴数据
	 */
	private void addAxis(final Object value){
		if(axis == null) axis = new ArrayList<Object>();
		if(!axis.contains(value)) axis.add(value);
	}
	
	public List<Object> getAxis() {
		return axis;
	}

	public void setAxis(List<Object> axis) {
		this.axis = axis;
	}
	
	public List<String> getLegend() {
		return legend;
	}

	public void setLegend(List<String> legend) {
		this.legend = legend;
	}
	
	public List<String> getTimeline() {
		return timeline;
	}

	public void setTimeline(List<String> timeline) {
		this.timeline = timeline;
	}
	
	public Map<String, Object> getSeries() {
		return series;
	}

	public void setSeries(Map<String, Object> series) {
		this.series = series;
	}

}
